package com.xhd.base.scheduler;

import java.util.HashMap;
import java.util.Vector;

import com.xhd.base.util.xBaseObj;
import com.xhd.cache.CacheMgr;
import com.xhd.cache.UseCache;
import com.xhd.config.SchedulerConfig;
import com.xhd.exception.SystemException;
import com.xhd.server.ServerConfig;
import com.xhd.servlet.ClientAction;

public class AsyncProcessMgr
implements UseCache
{
private Vector jm180 = null;
private Vector jm181 = null;
private int jm182 = ServerConfig.ThreadMax / 4;
private int jm183 = ServerConfig.ThreadMax / 20;
private HashMap jm184 = null;
private static AsyncProcessMgr jm16;

public static synchronized AsyncProcessMgr getInstance()
{
  if (jm16 == null)
    jm16 = new AsyncProcessMgr();
  return jm16;
}

private AsyncProcessMgr()
{
  CacheMgr.registerCache(this);
}

public void setMax(int paramInt1, int paramInt2)
{
  if (paramInt1 > 0)
    this.jm182 = paramInt1;
  if (paramInt2 <= 0)
    return;
  this.jm183 = paramInt2;
}

private synchronized boolean jm179(String paramString, boolean paramBoolean, int paramInt)
{
  Integer localInteger;
  if (paramBoolean)
  {
    localInteger = (Integer)this.jm184.get(paramString);
    if (localInteger == null)
      localInteger = new Integer(0);
    if (localInteger.intValue() >= paramInt)
      return false;
    localInteger = new Integer(1 + localInteger.intValue());
    this.jm184.put(paramString, localInteger);
  }
  else
  {
    localInteger = (Integer)this.jm184.get(paramString);
    if (localInteger != null)
      if (localInteger.intValue() > 1)
      {
        localInteger = new Integer(localInteger.intValue() - 1);
        this.jm184.put(paramString, localInteger);
      }
      else
      {
        this.jm184.remove(paramString);
      }
  }
  return true;
}

public synchronized void requestProcess(xBaseObj paramxBaseObj)
  throws SystemException
{
  ClientAction localClientAction = paramxBaseObj.getClientAction();
  if (this.jm180.size() >= this.jm182)
    throw new SystemException(4, String.valueOf(this.jm182));
  int i = SchedulerConfig.getInstance().getAsyncMax(paramxBaseObj.getClientAction());
  if (i > 0)
  {
  Object   localObject = paramxBaseObj.getUserInfo().getSysUserId() + "_" + localClientAction.getFullName();
    if (!(jm179((String)localObject, true, i)))
      throw new SystemException(5, String.valueOf(i));
  }
  Object localObject = null;
  if (this.jm181.size() > 0)
    localObject = (AsyncProcessThread)this.jm181.remove(0);
  else
    localObject = new AsyncProcessThread();
  this.jm180.add(localObject);
  ((AsyncProcessThread)localObject).setBizObj(paramxBaseObj);
}

protected void processFinish(AsyncProcessThread paramAsyncProcessThread)
{
  if (paramAsyncProcessThread == null)
    return;
  xBaseObj localxBaseObj = paramAsyncProcessThread.getBizObj();
  String str = localxBaseObj.getUserInfo().getSysUserId() + "_" + localxBaseObj.getClientAction().getFullName();
  jm179(str, false, 0);
  this.jm180.remove(paramAsyncProcessThread);
  if (this.jm181.size() < this.jm183)
    this.jm181.add(paramAsyncProcessThread);
  else
    paramAsyncProcessThread.stop = true;
}

public Vector getProcessList(int paramInt)
{
  int i = this.jm180.size();
  if (i > 0)
  {
    Vector localVector1 = new Vector(i);
    localVector1.addAll(this.jm180);
    i = localVector1.size();
    if (i < 1)
      return null;
    Vector localVector2 = new Vector(i);
    for (int j = 0; j < i; ++j)
    {
      AsyncProcessThread localAsyncProcessThread = (AsyncProcessThread)localVector1.get(j);
      xBaseObj localxBaseObj = localAsyncProcessThread.getBizObj();
      if (localxBaseObj == null)
        continue;
      ClientAction localClientAction = localxBaseObj.getClientAction();
      if (localClientAction == null)
        continue;
      if ((paramInt != 0) && (((localxBaseObj.getUserInfo() == null) || (paramInt != localxBaseObj.getUserInfo().getSysUserId()))))
        continue;
      localVector2.add(localxBaseObj);
    }
    localVector1 = null;
    return localVector2;
  }
  return null;
}

public Object cacheData()
{
  return this.jm180;
}

public int cacheSize()
{
  return this.jm180.size();
}

public boolean clearCache()
{
  return false;
}
}
